'use strict';

angular.module('app')
    .controller('zzyLaunchPlanController', [ '$rootScope', '$scope', '$http', '$state',
        function($rootScope, $scope, $http, $state) {
            $scope.title = '投放计划列表';
            $scope.searchParams = { };
            $scope.loading = false;
            $scope.param={};
            $scope.search = function () {
                $scope.loading = true;
                $scope.searchParams.putStartTime= $("#putStartTime").val();
                $scope.searchParams.putEndTime= $("#putEndTime").val();
                $scope.searchParams.createStartTime= $("#createStartTime").val();
                $scope.searchParams.createEndTime= $("#createEndTime").val();
                $scope.searchParams.examineStartTime= $("#examineStartTime").val();
                $scope.searchParams.examineEndTime= $("#examineEndTime").val();
                $scope.searchParams.planType= "2";
                $.ajax({
                    url : $scope.app.project_name+'/zzyLaunchPlan/read/list',
                    data: $scope.searchParams
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.pageInfo = result.data;
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }



            $scope.searchRe = function () {
                $scope.loading = true;
                $scope.searchParams.pageNum = 1;

                $scope.searchParams.putStartTime= $("#putStartTime").val();
                $scope.searchParams.putEndTime= $("#putEndTime").val();
                $scope.searchParams.createStartTime= $("#createStartTime").val();
                $scope.searchParams.createEndTime= $("#createEndTime").val();
                $scope.searchParams.examineStartTime= $("#examineStartTime").val();
                $scope.searchParams.examineEndTime= $("#examineEndTime").val();
                $scope.searchParams.planType= "2";
                $.ajax({
                    url : $scope.app.project_name+'/zzyLaunchPlan/read/list',
                    data: $scope.searchParams
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.pageInfo = result.data;
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }

            $scope.selected = [];
            var updateSelected = function (action, contact) {
                if (action == 'add' && $scope.selected.indexOf(contact.id) == -1){
                    $scope.selected.push(contact.id);
                    contact.checked = true;
                }

                if (action == 'remove' && $scope.selected.indexOf(contact.id) != -1){
                    $scope.selected.splice($scope.selected.indexOf(contact.id), 1);
                    contact.checked = false;
                }
            };

            $scope.updateSelection = function ($event, contact) {
                var checkbox = $event.target;
                var action = (checkbox.checked ? 'add' : 'remove');
                if (action == 'add' && $scope.selected.indexOf(contact.id) == -1){
                    $scope.selected.push(contact.id);
                    contact.checked = true;
                }
                if (action == 'remove' && $scope.selected.indexOf(contact.id) != -1){
                    $scope.selected.splice($scope.selected.indexOf(contact.id), 1);
                    contact.checked = false;
                }
            };
            // 全选操作
            $scope.selectAll = function ($event) {
                var checkbox = $event.target;
                var action = (checkbox.checked ? 'add' : 'remove');
                for (var i = 0; i < $scope.pageInfo.records.length; i++) {
                    var contact = $scope.pageInfo.records[i];
                    updateSelected(action, contact);
                }
            };

            $scope.isSelected = function (id) {
                return $scope.selected.indexOf(id) >= 0;
            };


            $scope.search();

            $scope.clearSearch = function() {
                $("#putStartTime").val("");
                $("#putEndTime").val("");
                $("#createStartTime").val("");
                $("#createEndTime").val("");
                $("#examineStartTime").val("");
                $("#examineEndTime").val("");
                $scope.searchParams= { };
                $scope.search();
            }



            // 翻页
            $scope.pagination = function (pageNo) {
                $scope.searchParams.pageNum=pageNo;
                $scope.search();
            };




            //批量通过/失败
            $scope.batchApprove=function (state) {
                if($scope.selected.length<=0){
                    $scope.alertModal({"content":"请选择要审核的项"});
                    return;
                }
                //判断是否有待审核的并记录有多少条
                var count = 0;
                var nocount = 0;
                angular.forEach($scope.pageInfo.records,function (item) {
                    if(item.status=='2'&&item.checked){
                        count++;
                    }else if(item.checked&&item.status!='2') {
                        nocount++;
                    }
                })
                if(nocount>0){
                    $scope.alertModal({"content":"待审核"+count+"条,不符合操作条件的"+nocount+"条"});
                    return;
                }
                $scope.alertMessage({
                    text:"确定做此操作吗？", //内容
                    btnCancel:true, //取消按钮
                    btnConfirm:true //确认按钮
                }).then(function () {
                    if(state==3){
                        $.ajax({
                            url : $scope.app.project_name + '/launchPlan/batchApprove',
                            data: {'ids':$scope.selected, 'state' : state}
                        }).then(function(result) {
                            $scope.loading = false;
                            if (result.httpCode == 200) {
                                $('#refuseUserSupply').modal('hide');
                                $scope.searchParams.keyword= null;
                                $scope.selected = [];
                                $scope.search();
                                $scope.alertModal({"content":"审核成功"});
                            } else {
                                $('#refuseUserSupply').modal('hide');
                                $scope.alertModal({"content":"审核失败"});
                            }
                        });
                    }else{
                        $scope.param = {};
                        $scope.param.title ="推广计划";
                        $("#title").text($scope.param.title);
                        $('#refuseUserSupply').modal('show', true);
                    }
                })
            }

            $scope.saveApproveReason = function () {
                var reason = $scope.param.approveReason;
                if(reason==''||reason==null){
                    $scope.alertModal({"content":"请输入拒绝原因"});
                    $('#refuseUserSupply').modal('hide');
                    return;
                }

                $.ajax({
                    url : $scope.app.project_name + '/launchPlan/batchApprove',
                    data: {'ids':$scope.selected,'state':4,'reason':reason}
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $('#refuseUserSupply').modal('hide');
                        $scope.searchParams.keyword= null;
                        $scope.selected = [];
                        $scope.search();
                        $scope.alertModal({"content":"审核成功"});
                    } else {
                        $('#refuseUserSupply').modal('hide');
                        $scope.alertModal({"content":"审核失败"});
                    }
                });
            }

        } ]);